package work20241226;

/**
 * @author zdl
 * @date 2024/12/26
 */
class Count implements Runnable {
    int count = 1;

    @Override
    public void run() {
        while (count <= 1000){
            synchronized (this) {
                if (count <= 1000) {
                    System.out.println(Thread.currentThread().getName() + "线程维修第" + count + "次");
                    count++;
                }
            }
        }
    }
}

public class Demo12 {
    public static void main(String[] args) {
        //12. 编写一个程序，使用同步方法确保在多个线程中对共享资源进行安全的操作，例如维护使用1000次就失效的计数器。
        Count count = new Count();
        new Thread(count,"线程1").start();
        new Thread(count,"线程2").start();
        new Thread(count,"线程3").start();
        new Thread(count,"线程4").start();
        new Thread(count,"线程5").start();
        new Thread(count,"线程6").start();
        new Thread(count,"线程7").start();
        new Thread(count,"线程8").start();
        new Thread(count,"线程9").start();
        new Thread(count,"线程10").start();
        new Thread(count,"线程11").start();
        new Thread(count,"线程12").start();
        new Thread(count,"线程13").start();

    }
}
